package com.ltcode.lock.starter.handle;
import com.ltcode.lock.starter.utils.LockUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class LockHandle {
    private static final Logger log = LoggerFactory.getLogger(LockHandle.class);

    /**
     * 处理方法
     * @throws Exception
     */
   public void handle(String lockKey,LockConsumer lockConsumer) {
       try{
           if(lockKey==null ||"".equals(lockKey)){
               lockConsumer.process();
                return;
           }
           LockUtil.lock(lockKey);
           lockConsumer.process();
       } catch (Exception e) {
           log.error("invoke process exception");
           throw new RuntimeException(e);
       } finally {
            if(lockKey!=null&&!"".equals(lockKey)&&LockUtil.isLocked(lockKey)){
                LockUtil.unlock(lockKey);
            }
       }
   }

}
